//
//  ShowVideoViewController.m
//  JiuwanBox
//
//  Created by lanou3g on 15/6/3.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ShowVideoViewController.h"

@interface ShowVideoViewController ()

@end

@implementation ShowVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self addNavigationBar];
    [self showHud];
    
        self.firstView.delegate=self;
        self.firstView.dataSource=self;
        self.secondView.delegate=self;
        self.secondView.dataSource=self;
    
        [self readVideo];
    
        self.video=_videoArray[0];
        [self readMatch];
    self.personArray=[NSArray array];
    self.personArray=@[@"文森特",@"瑞文",@"小智",@"盲僧",@"徐老师",@"蛮王",@"OMG",@"寒冰",@"若风",@"小苍教学",@"草莓解说"];
    
}

//未加载时显示加载提示
-(void)showHud{
    //大菊花展
    self.hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.hud];
    self.hud.labelText=@"拼命加载中";
    [self.hud show:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//设置导航栏控件,searchBar
-(void)addNavigationBar{
    //设置导航栏图片
    UIImage *image=[UIImage imageNamed:@"lol0.png"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTranslucent:NO];//透明度
//添加搜索按钮
    [self.searchButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:normal];
    [self.searchButton addTarget:self action:@selector(searchVideo) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem*leftBI=[[UIBarButtonItem alloc]initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:@selector(reload:)];

    self.navigationItem.leftBarButtonItem=leftBI;
    leftBI.tintColor=[UIColor yellowColor];
    
    self.searchBar.barStyle = UIBarStyleDefault;
    self.searchBar.translucent = YES;//半透明
    self.searchBar.placeholder = @"搜索想看的视频";
    
    self.searchBar.delegate =self;

   
    
    [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"cancel"] forState:normal];
    [self.cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
}
//编辑搜索框
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
 [self layoutDropDown];//布局下拉框
    self.isShowList=YES;
    
    return YES;

}

-(void)layoutDropDown{
    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(30, 30, CGRectGetWidth(self.searchBar.bounds), 200)];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
   
    self.tableView.tag=122;
    [self.view addSubview:self.tableView];
    [self.view bringSubviewToFront:self.tableView];
}



//实现搜索按钮
-(void)searchVideo{

    SearchViewController*searchVC=[[SearchViewController alloc]initWithStyle:UITableViewStylePlain];
       searchVC.str=self.searchBar.text;//传值(搜索)
    
       //点击搜索回收键盘
    [_searchBar resignFirstResponder];
      self.searchBar.text=@"";
    if(self.isShowList==YES){
        [self.tableView removeFromSuperview];
        self.isShowList=NO;
    }
    
       [self.navigationController pushViewController:searchVC animated:YES];
   


}
//点击搜索框上的取消按钮时 调用
- (void)cancel{
   
    _searchBar.text = @"";
    [_searchBar resignFirstResponder];
    if(self.isShowList==YES){
    [self.tableView removeFromSuperview];//取消下拉框
        self.isShowList=NO;
    }
}
//切屏时回收下拉框
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    if(self.isShowList==YES){
        [self.tableView removeFromSuperview];
        self.isShowList=NO;
    }


}
//防止从别的页面切屏过来
-(void)viewWillAppear:(BOOL)animated{
    if(self.isShowList==YES){
        [self.tableView removeFromSuperview];
        self.isShowList=NO;
    
    
    }
}
//点击其他区域键盘回收
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.searchBar resignFirstResponder];
}
//刷新
-(void)reload:(UIBarButtonItem*)BI{
    if(self.isShowList==YES){
    
        [self.tableView removeFromSuperview];
        self.isShowList=NO;
    }
    [self.searchBar resignFirstResponder];
        [self readVideo];
        self.video=_videoArray[0];
        [self readMatch];
    }

-(void)readVideo{
        NSURL*url=[NSURL URLWithString:VideoList];
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
    
        NSData*data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
        if(data==nil){
                return;
            }
            NSMutableDictionary*dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSMutableArray*array=[dic valueForKey:@"result"];
            self.videoArray=[NSMutableArray array];
            for(NSMutableDictionary*dict in array){
                Video*video=[[Video alloc]init];
                [video setValuesForKeysWithDictionary:dict];
    
                [self.videoArray addObject:video];
            }
      // NSLog(@"%@",self.videoArray);
            [self.firstView reloadData];
        [self.hud removeFromSuperview];
    
    }
    -(void)readMatch{
    
        NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:MatchUrl,self.video.Id]];
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        __weak ShowVideoViewController* showVC=self;
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]  completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if(data==nil){
                return;
            }
            NSMutableDictionary*dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSMutableArray*array=[dic valueForKey:@"result"];
            showVC.matchArray=[NSMutableArray array];
            for(NSMutableDictionary*dict in array){
                Match*match=[[Match alloc]init];
                [match setValuesForKeysWithDictionary:dict];
    
                [showVC.matchArray addObject:match];
            }
         // NSLog(@"%@",_matchArray);
            [self.secondView reloadData];
        }];
    
    
    
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if(tableView.tag==120){
           //添加背景图片
           UIImageView*imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lol3"]];
           tableView.backgroundView=imgView;
    
            return self.videoArray.count;
    
        }
        if(tableView.tag==121){
        //添加背景图片
           UIImageView*imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lol4"]];
           tableView.backgroundView=imgView;
            return self.matchArray.count;
        }
        if(tableView.tag==122){
            return self.personArray.count;
        }
    
        return 0;
    }
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
        return 1;
    
    
    
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if(tableView.tag==120){
            UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"videoCell"];
            if(!cell){
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"videoCell"];
    
            }
            Video* video=_videoArray[indexPath.row];
            cell.textLabel.text=video.name;
           cell.textLabel.tintColor=[UIColor greenColor];
            //设置字体类型
            cell.textLabel.font =Font_18;
            cell.textLabel.numberOfLines=0;
           //透明
           cell.backgroundColor=[UIColor clearColor];
           //设置选中cell的背景图片
           cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lol5"]];
            //设置选中cell的字体颜色
           cell.textLabel.highlightedTextColor = [UIColor redColor];
            return cell;
        }
        if(tableView.tag==121){
            UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"matchCell"];
            if(!cell){
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"matchCell"];
    
            }
            Match*match=_matchArray[indexPath.row];
            cell.textLabel.text=match.name;
            //改汉字字体
            cell.textLabel.font=Font_15;
            cell.textLabel.numberOfLines=0;
           //透明
           cell.backgroundColor=[UIColor clearColor];
    
           //设置选中cell的背景图片
           cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lol5"]];
           cell.textLabel.highlightedTextColor = [UIColor redColor];
    
           return cell;
    
    
        }
        if(tableView.tag==122){
           UIColor *color=[UIColor colorWithRed:236/255.0 green:246/255.0 blue:221/255.0 alpha:1];
            tableView.backgroundColor=color;
            UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"personCell"];
            if(!cell){
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"personCell"];
                
            }

            cell.textLabel.text=self.personArray[indexPath.row];
            cell.textLabel.font=Font_15;
            cell.textLabel.textColor=[UIColor redColor];
            cell.backgroundColor=[UIColor clearColor];
            return cell;
        }
        return nil;
    
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if(tableView.tag==120){
        
           
           self.video=self.videoArray[indexPath.row];
          
            [self readMatch];
        
        }
    
        if(tableView.tag==121){
            Match*match=self.matchArray[indexPath.row];
           
          
           
            MatchDetailViewController*matchDVC=[[MatchDetailViewController alloc]init];
            matchDVC.match=match;
            [self.navigationController pushViewController:matchDVC animated:YES];
           
        
          
        }
        if(tableView.tag==122){
            self.searchBar.text=self.personArray[indexPath.row];
            [self.searchBar resignFirstResponder];
           
            [self.tableView removeFromSuperview];
            self.isShowList=NO;
        }
    
    }

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView.tag==120){
        return 60;
    }
    if(tableView.tag==121){
        return 50;
    }
    if(tableView.tag==122){
        return 20;
    }
    return 10;//无用
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
